﻿ using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Models;
using Newtonsoft.Json;

namespace WeiBoWeb.Controllers
{
    public class WeiBoController : Controller
    {

        // GET: WeiBo
        public ActionResult Weibo()
        {
            int Uid = Convert.ToInt32( Session["UserId"]);
            
            //第一个事件进行时加载“我的微博”内容
            ViewBag.content = WeiBoManager.SelectMyWbContent(Uid);//微博内容
            ViewBag.Info= WeiBoManager. SelectUserInfo(Uid);  //资料
            ViewBag.Label = WeiBoManager.SelectUserLabel(Uid); //标签


            ViewBag.Follow = WeiBoManager.SelectUserFollow(Uid);//我的关注
            List<dynamic> list = new List<dynamic>();  //该列表存储关注的人的ID
            foreach (var item in ViewBag.Follow)
            {
                dynamic d = new ExpandoObject();
                d.U_ID = item.U_ID;
                list.Add(d);
            }

            using (MicroBlogEntities db=new MicroBlogEntities())  //关注与粉丝的数量
            {
                //ViewBag.Count_WeiBo =db.MicroBlog.Where(m => m.U_ID== Uid).Count(c => c.M_ID);
                ViewBag.Count_WeiBo = (from m in db.MicroBlog where m.U_ID == Uid select m.M_ID).Count();
                ViewBag.Count_Follow = (from f in db.UserFollow where f.U_ID == Uid select f.FollowID).Count();
                ViewBag.Count_Fans = (from s in db.UserFans where s.U_ID == Uid select s.FansID).Count();
            }
            return View();
        }

        //分部视图微博回复的方法
        public ActionResult WordBack1()
        {
            int id = Convert.ToInt32( Request["WbID"].ToString());
            ViewBag.comment = WeiBoManager.SelectMyWbComment(id);

            int Uid = Convert.ToInt32(Session["UserId"]);
            List<dynamic> content = WeiBoManager.SelectMyWbContent(Uid);
            ViewBag.content = content;

            return PartialView("_mainBannerContent2PeopleWordBack1");
        }

        ////查看关注的用户模态框效果方法
        //public ActionResult SelectFollowInfo(int? FUid)
        //{

        //    ViewBag.FollowInfo2 = WeiBoManager.SelectUserInfo(FUid);  //资料
        //    using (MicroBlogEntities db = new MicroBlogEntities())  //关注与粉丝的数量
        //    {
        //        ViewBag.Count_WeiBo2 = (from m in db.MicroBlog where m.U_ID == FUid select m.M_ID).Count();
        //        ViewBag.Count_Follow2 = (from f in db.UserFollow where f.U_ID == FUid select f.FollowID).Count();
        //        ViewBag.Count_Fans2 = (from s in db.UserFans where s.U_ID == FUid select s.FansID).Count();
        //    }
        //    return Content("true");
        //}


        [HttpPost]
        //添加标签的方法
        public ActionResult AddUserLabel()
        {

            int Uid = Convert.ToInt32(Session["UserId"]);
            string txt = Request.Form["txt_departmentname"];
            UserLable userLable = new UserLable {
                U_ID = Uid,
                UL_Content = txt
            };
            if (WeiBoManager.InsertLabel(userLable))
            {
                List<dynamic> Label = WeiBoManager.SelectUserLabel(Uid);
                ViewBag.Label = Label;
            }
            return PartialView("_UserLabel");
        }
        //取消关注
        public ActionResult DeleteFollow(int? FUid)
        {
            int Uid = Convert.ToInt32(Session["UserId"]);
            if (WeiBoManager.DeleteFollow(FUid))
            {                
                ViewBag.Follow = WeiBoManager.SelectUserFollow(Uid);//我的关注                
            }
            return PartialView("_DeleteFollow");
        }

        //查看用户关注 
        public ActionResult SelectUserFollow()
        {
            return View();
        }
        //弹出层查看用户粉丝 
        public ActionResult SelectCountFans()
        {
            return View();
        }

        //发布微博的方法
        public ActionResult BuildWb()
        {         
            return View();
        }
        
        public ActionResult MyFollowInfo(int FUid)
        {
            ViewBag.FollowInfo2 = WeiBoManager.SelectUserInfo(FUid);  //资料
            var FollowInfo = ViewBag.FollowInfo2;
            using (MicroBlogEntities db = new MicroBlogEntities())  //关注与粉丝的数量
            {
                //ViewBag.Count_WeiBo =db.MicroBlog.Where(m => m.U_ID== Uid).Count(c => c.M_ID);
                ViewBag.Count_WeiBo2 = (from m in db.MicroBlog where m.U_ID == FUid select m.M_ID).Count();
                ViewBag.Count_Follow2 = (from f in db.UserFollow where f.U_ID == FUid select f.FollowID).Count();
                ViewBag.Count_Fans2 = (from s in db.UserFans where s.U_ID == FUid select s.FansID).Count();
            }
            return PartialView("_MyFollowInfo", FollowInfo);
        }

    }
}